package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.scf.dto.ScfRenewableOutInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfRenewableOutInfo;
import com.dhcc.bpm.modules.scf.service.IScfRenewableOutInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableOutInfoVO.ScfGetRenewableOutDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableOutInfoVO.ScfGetRenewableOutListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 再生资源出库控制器
 *
 * @author liuguoting
 * @since 2023-09-22
 */
@RestController
@AllArgsConstructor
public class ScfRenewableOutInfoController extends BladeController {

    private IScfRenewableOutInfoService scfRenewableOutInfoService;


    /**
     * 查询再生资源出库列表
     *
     * @param scfRenewableOutInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetRenewableOutList")
    // @Cacheable(value = "scfRenewableOutListCache", key = "'scfRenewableOutListCacheKey-' + #scfRenewableOutInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetRenewableOutListVO>> pageList(ScfRenewableOutInfoDTO scfRenewableOutInfo, Query query) {

        if (StringUtils.isEmpty( scfRenewableOutInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfRenewableOutInfo.setOrgCodes(  scfRenewableOutInfo.getOrgCode().split(",") );

        if(scfRenewableOutInfo.getOrgCode().contains( "50000000")){
            scfRenewableOutInfo.setOrgCode("50000000");
            scfRenewableOutInfo.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfRenewableOutInfoService.selectScfRenewableOutInfoPage(Condition.getPage(query), scfRenewableOutInfo));
    }

    /**
     * 查询再生资源出库单详情
     *
     * @param scfRenewableOutInfo
     * @return
     */
    @GetMapping("/scfGetRenewableOutDetails")
    // @Cacheable(value = "scfRenewableOutDetailsCache", key = "'scfRenewableOutDetailsCacheKey-' + #scfRenewableOutInfo.rowId")
    public R<ScfGetRenewableOutDetailsVO> details(ScfRenewableOutInfoDTO scfRenewableOutInfo) {
        return R.data(scfRenewableOutInfoService.getDetails(scfRenewableOutInfo));
    }

    /**
     * 新增再生资源出库信息
     *
     * @param scfRenewableOutInfo
     * @return
     */
    @PostMapping("scfAddRenewableOutDetails")
    // @CacheEvict(value = "scfRenewableOutListCache", allEntries = true)
    public R<Boolean> saveWarehouseEnt(@RequestBody ScfRenewableOutInfoDTO scfRenewableOutInfo) throws Exception {
        return R.status(scfRenewableOutInfoService.saveRenewableOutInfo(scfRenewableOutInfo));
    }

    /**
     * 修改再生资源出库信息
     *
     * @param scfRenewableOutInfo
     * @return
     */
    @PostMapping("scfModRenewableOutDetails")
    // @CacheEvict(value = "scfRenewableOutDetailsCache", key = "'scfRenewableOutDetailsCacheKey-' + #scfRenewableOutInfo.rowId")
    public R<Boolean> updateWarehouseEnt(@RequestBody ScfRenewableOutInfoDTO scfRenewableOutInfo) {
        return R.status(scfRenewableOutInfoService.updateRenewableOutInfo(scfRenewableOutInfo));
    }

    /**
     * 批量导入再生资源出库信息
     *  导入出库单
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfRenewableOutFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfRenewableOutInfo> scfRenewableOutInfos = scfRenewableOutInfoService.batchSaveRenewableOutInfo(operatorId, file);
        int bachSaveRenewableOutInfo = scfRenewableOutInfoService.bachSaveRenewableOutInfo(scfRenewableOutInfos, operatorId);
        int total = scfRenewableOutInfos.size();
        int totalSussess = bachSaveRenewableOutInfo;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
//        baseMapper.saveRenewableOutByRenewableInt(renewableOutByRenewableIn);
        return R.success(msg);
    }



    @PostMapping("/scfRenewableOutInfo/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfRenewableOutInfoService.deleteByIds( split ));
    }

}
